import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/20 20:45
 * @description 深度优先遍历
 */
public class Solution {
    public static void main(String[] args) {

    }
    int ans = 0;
    public int distributeCoins(TreeNode root) {
        dfs(root);
        return ans;
    }
    public int dfs(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int L = dfs(node.left);
        int R = dfs(node.right);
        ans += Math.abs(L) + Math.abs(R);
        return node.val + L + R - 1;
    }
}
